<?php
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
/*
	[天生创想] (C)2008-2011 515158 Inc.
	This is NOT a freeware, use is subject to license terms
    URL:http://www.515158.com/
*/
get_config_keytable("department_");

empty($do) && $do = 'list';
if ($do == 'list') {
	$id = getGP('documentid','G','int');
	$rs = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."department  WHERE id = '$id'");
	include_once('template/index.php');

}elseif ($do == '保 存') {
	$savetype = getGP('savetype','P');
	$id = getGP('id','P','int');
	$name = getGP('name','P');
	$persno = getGP('persno','P');
	$father = getGP('father','P');
	$date = get_date('Y-m-d H:i:s',PHP_TIME);
	if($name!="" && $persno!=''){
	$department = array(
		'name' => $name,
		'persno' => $persno,
		'father' => $father,
		'date' => $date
	);
	
	if($savetype=='add'){
	insert_db('department',$department);
	}else{
	//echo $id;
	//exit;
	update_db('department',$department, array('id' => $id));
	}
	goto_page('admin.php?ac=index&fileurl=department');
	}else{
	goto_page('admin.php?ac=index&fileurl=department');
	}

}elseif ($do == '删 除') {
	$idarr = getGP('id','P','array');
	foreach ($idarr as $id) {
	$db->query("DELETE FROM ".DB_TABLEPRE."department WHERE id = '$id'");
	//db->query("DELETE FROM ".DB_TABLEPRE."user_view WHERE uid = '$id'");
	}
	goto_page('admin.php?ac=index&fileurl=department&message=1');

} 

///////////////////////////////////////////////////////////////////

function get_document_list($fatherid=0,$selid=0,$layer=0)
{

    
    global $db;
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."department where father='$fatherid'  ORDER BY id Asc  ");
	
	if(count($query)>0){
	   //for($i=0;$i<$layer;$i++){
	   
	   //$str.="<TD class=MemoTD width=20>&nbsp;</TD>";
	   
	   //}
	while ($row = $db->fetch_array($query)) {
	$rsfno = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."department where father='".$row[id]."'  ORDER BY id asc limit 0,1");

	echo "a.add(".$row[id].",".$fatherid.",'".$row[name]."','admin.php?ac=index&fileurl=department&documentid=".$row[id]."');";

	if($rsfno[id]!=''){
	get_document_list($row['id'],$selid,$layer+1);
	}
	}
	}
	
   return ;

}
//================================================================
function GET_document_PUBLIC_LIST($fatherid=0,$selid=0,$layer=0)
{


	$str="";
    global $db;
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."department where father='$fatherid' ORDER BY id Asc  ");
	
	if(count($query)>0){
	   for($i=0;$i<$layer;$i++){
	   
	   $str.="├";
	   }
	while ($row = $db->fetch_array($query)) {

	$selstr = $row['id'] == $selid ? 'selected="selected"' : '';
	
	$htmlstr= '<option value="'.$row['id'].'"  '.$selstr.'>'.$str.$row['name'].'</option>';
	
	echo $htmlstr;
    
	GET_document_PUBLIC_LIST($row['id'],$selid,$layer+1);
	
	}

	}
	
   return ;

}


?>